package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.itheima")
//@Import(MybatisConfig_bak.class)
@Import(MybatisConfig.class)
@PropertySource(value = "classpath:jdbc.properties",encoding = "utf-8")//加载jdbc配置文件
@MapperScan("com.itheima.dao")//开启mapper接口扫描功能
public class SpringConfig {

    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.driver}")
    private String driverClass;

    /**
     * 构建数据源对象
     * @return
     */
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }

}
